package common.base
{
	import com.common.stdlib.ArrayUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.model.type.GoodsType;
	import com.liangzhong.model.type.OtherGoodType;
	import com.lz.football.module.base.IComponent;
	
	import common.item.GoodsUseItem;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import lz.controls.Alert;
	import lz.controls.PageComponent;
	import lz.controls.core.TitleWindow;
	import lz.controls.events.ListEvent;
	import lz.controls.events.PageEvent;
	import lz.controls.managers.TipsManager;

	/**
	 *物品使用小面板 
	 * @author lake
	 * //测试
			var goodsUsePanel :GoodsUsePanel = new GoodsUsePanel();
			goodsUsePanel.initUI();
			this.addChild(goodsUsePanel);
			//取士气卡
			goodsUsePanel.goodsList = AppData.getInstance().bagDataManager.getGoodsListByTwoTypes(GoodsType.OTHER,OtherGoodType.MORALE);
	 */	
	public class GoodsUsePanel extends TitleWindow implements IComponent
	{
		/**
		 * 显示购买和欧元按钮1
		 */
		public static const BTN_TYPE_1:int = 1;
		
		/**
		 * 2 显示点券增加和使用按钮
		 */
		public static const BTN_TYPE_2:int = 2;
		/**
		 * 3 购买和使用
		 */
		public static const BTN_TYPE_3:int = 3;
		/**
		 * 4欧元按钮
		 */
		public static const BTN_TYPE_4:int = 4;
		/**
		 * 5使用按钮
		 */
		public static const BTN_TYPE_5:int = 5;
		/**
		 *6 欧元治疗 
		 */		
		public static const BTN_TYPE_6:int = 6;
		/**
		 * 7欧元恢复 
		 */
		public static const BTN_TYPE_7:int = 7;
		
		private static var _inistance:GoodsUsePanel;
		/**
		 *第一格子皮肤  guide 
		 */		
		public static var firstItemSkin :DisplayObject;
		/**
		 *物品容器 
		 */		
		private var container 		:MovieClip;
		/**
		 *购买按钮 
		 */		
		private var buyBtn			:SimpleButton;
		/**
		 *欧元按钮 
		 */		
		private var euroBtn		:SimpleButton;
		/**
		 *点券增加按钮  
		 */
		private var coinBtn		:SimpleButton;
		/**
		 *使用按钮  
		 */
		public static var useBtn	:SimpleButton;
		
		/**
		 *欧元治疗 
		 */
		private var treatBtn:SimpleButton;
		
		/**
		 *欧元恢复 
		 */
		private var recoverBtn:SimpleButton;
		/**
		 *分页 
		 */		
		private var pageComp 		:PageComponent ;
		/**
		 *item 列表 
		 */		
		private var itemList		:Array = [];
		/**
		 *要显示的物品列表 
		 */		
		private var _goodsList		:Array = [];
		/**
		 *选择的条目 
		 */		
		private var selectItem		:GoodsUseItem;
		private var _callBack:Function;
		/**
		 *物品主类型 
		 */
		private var type:int;
		/**
		 * 副类型
		 */
		private var nType:int;
		/**
		 *物品ID 
		 */
		private var pid:int;
		/**
		 *显示的按钮类型 
		 */
		private var _btnType:int;
		public function GoodsUsePanel()
		{
			super();
		}
		/**
		 *增加物品 
		 * @param goodsVo
		 * 
		 */		
		public function addGoods(goodsVo :GoodsVo):void
		{
			_goodsList.push(goodsVo);
			_goodsList.sortOn(["ntype","color"],[Array.NUMERIC,Array.NUMERIC]);
			pageComp.curPage = pageComp.curPage;
		}
		/**
		 *删除物品 
		 * @param goodsVo
		 * 
		 */		
		public function removeGoods(goodsVo :GoodsVo):void
		{
			_goodsList.splice(_goodsList.indexOf(goodsVo),1);
			pageComp.curPage = pageComp.curPage;
		}
		/**
		 *初始化 
		 * @param view
		 * 
		 */		
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.common.GoodsUsePanelSkin");
			if(_symbolSkin == null)
			{
				return ;
			}
			this.addChild(_symbolSkin);
			//背景框
			windowBg = _symbolSkin.windowBg;
			//格子容器
			container = _symbolSkin.container;
			euroBtn = _symbolSkin.euroBtn;
			coinBtn = _symbolSkin.coinBtn;
			useBtn = _symbolSkin.useBtn;
			buyBtn = _symbolSkin.BuyBtn;
			treatBtn = _symbolSkin.treatBtn;
			recoverBtn  =_symbolSkin.recoverBtn;
			//设置可拖动
			dragEnabled = true;
			//设置关闭按钮
			setCloseBtn(_symbolSkin.closeBtn);
			
			//设置坐标不自适应
			resizeEnabled = false;
			//分页初始化
			pageComp = new PageComponent();
			pageComp.initUI(_symbolSkin.pageComp);
			//初始化格子
			var i :int = 0;
			var uItem :GoodsUseItem;
			for(;i<18;i++)
			{
				uItem = new GoodsUseItem();
				uItem.initUI();
				uItem.x = (i % 6) * 58;
				uItem.y = int(i / 6) * 58;
				container.addChild(uItem);
				itemList.push(uItem);
			}
			addEvtListener();
			showBtn();
			//@lake 2013-11-16
			firstItemSkin = itemList[0];
			
		}
		/**
		 *初始化位置 
		 * 添加 到GlobalDef.GameContainer
		 * 添加addEvtListener
		 * 设置位置move中间
		 * 还没有添加到舞台是位置设置在中间
		 * 已经添加了不改变位置
		 */
		public function initPlace():void
		{
			_btnType = 1;
			addEvtListener();
			if(!this.parent)
				move((GlobalDef.StageWidth - windowBg.width) >> 1,(GlobalDef.StageHeight - windowBg.height)>> 1);
			GlobalDef.GameWindowContainer.addChild(this);
		}
		/**
		 *按钮显示类型 
		 * @param $type
		 * 1 显示购买和欧元按钮
		 * 2 显示点券增加和使用按钮
		 * 3 购买和使用
		 */
		public function showBtn($type:int = 1):void
		{
			_btnType = $type;
			buyBtn.visible  = false;
			euroBtn.visible = false;
			coinBtn.visible = false;
			useBtn.visible = false;
			treatBtn.visible = false;
			recoverBtn.visible = false;
			switch($type)
			{
				case BTN_TYPE_1:
					buyBtn.visible  = true;
					euroBtn.visible = true;
					break;
				case BTN_TYPE_2:
					coinBtn.visible = true;
					useBtn.visible = true;
					break;
				case BTN_TYPE_3:
					buyBtn.visible  = true;
					useBtn.visible = true;
					break;
				case BTN_TYPE_4:
					euroBtn.visible = true;
					break;
				case BTN_TYPE_5:
					useBtn.visible = true;
					break;
				case BTN_TYPE_6:
					treatBtn.visible = true;
					break;
				case BTN_TYPE_7:
					recoverBtn.visible = true;
					break;
			}
			pageComp.curPage = 1;
		}
		/**
		 *设置物品显示类型 
		 * 
		 */
		public function setShowGoodsType($type:int,$nType:int = 0):void
		{
			type = $type;
			nType = $nType;
			goodsList = AppData.getInstance().bagDataManager.getGoodsListByTwoTypes(type,nType);
		}
		
		/**
		 *设置体力显示物品 
		 * 
		 */
		public function setshowphysical($type:int,$nType:int = 0,$id:int = 0):void
		{
			type = $type;
			nType = $nType;
			pid = $id;
			var temp:Array = [];
			var arr:Array = AppData.getInstance().bagDataManager.getGoodsListByTwoTypes(type,nType);
			for each(var goods :GoodsVo in arr)
			{
				if(goods.gid == pid)
				{
					temp.push(goods);
				}
			}
			goodsList = temp;
		}
		/**
		 *设置获取商品列表 
		 * @param value
		 * 
		 */		
		public function set goodsList(value :Array):void
		{
			_goodsList = value;
			if(_goodsList == null)
			{
				return;
			}
			//分页设置
			pageComp.totalNumbers = _goodsList.length;
			pageComp.pageSize = 18;
			pageComp.curPage = pageComp.curPage;
			showBtn(_btnType);
		}
		public function get goodsList():Array
		{
			return _goodsList;
		}
		/**
		 *设置数据 
		 * @param datas
		 * 
		 */		
		public function setItemPageDatas(datas :Array):void
		{
			var item :GoodsUseItem; 
			var i :int = 0;
			for each(var itemObj :* in datas)
			{
				if(i>=18)return;
				item = itemList[i];
				item.data = itemObj;
				container.addChild(item);
				i ++;
			}
			for(;i < 18;i++)
			{
				item = itemList[i];
				item.data = null;
			}
			//			showBtn();
		}
		/**
		 *添加事件 
		 * 
		 */		
		public function addEvtListener():void
		{
			addEvent();
			pageComp.addEventListener(PageEvent.PAGE_CHANGE,pageChangeHandler);
			this.addEventListener(ListEvent.ITEM_CLICK,itemClickHandler);
			this.addEventListener(ListEvent.ITEM_DOUBLE_CLICK,itemDoubleClickHandler);
			euroBtn.addEventListener(MouseEvent.CLICK,onEuroHandler);
			
			treatBtn.addEventListener(MouseEvent.CLICK,onEuroHandler);
			recoverBtn.addEventListener(MouseEvent.CLICK,onEuroHandler);
			this.addEventListener(Event.REMOVED_FROM_STAGE,onremoveStageHandler);
			coinBtn.addEventListener(MouseEvent.CLICK,onCoinHandler);
			useBtn.addEventListener(MouseEvent.CLICK,onUseItemHandler);
			AppData.getInstance().bagDataManager.addEventListener(Event.CHANGE,onGoodsChange);
		}
		
		
		/**
		 * 物品数量改变
		 * @param evnet
		 * 
		 */
		private function onGoodsChange(evnet:Event):void
		{
//			goodsList = AppData.getInstance().bagDataManager.getGoodsListByTwoTypes(type,nType);
//			if(type == -1)return;
			if(type == GoodsType.OTHER && nType == OtherGoodType.PHYSICAL)
			{
				setshowphysical(type,nType,pid);
			}else
			{
				setShowGoodsType(type,nType);
			}
				
		}
		/**
		 *使用按钮 
		 * @param event
		 * 
		 */
		protected function onUseItemHandler(event:MouseEvent):void
		{
			if(selectItem != null)
			{
				if(_callBack != null)_callBack(selectItem.data as GoodsVo);
			}
			else
			{
				Alert.showErrorTip("请先选择使用的物品");
			}
			//@lake 2013-11-16
			AppEventDispatcher.getInstance().dispatchEvt("Guide_use_item");
		}
		
		
		
		/**
		 *从舞台中移除自己 
		 * @param event
		 * 
		 */
		protected function onremoveStageHandler(event:Event):void
		{
			_callBack = null;
			type = 0;
			nType = 0;
			removeEvtListener();
		}
		
		/**
		 *消耗欧元 
		 * @param event
		 * 
		 */
		protected function onEuroHandler(event:MouseEvent):void
		{
			if(_callBack != null)_callBack(null);
		}
		/**
		 * 消耗点券 
		 * @param event
		 * 
		 */
		protected function onCoinHandler(event:MouseEvent):void
		{
			if(_callBack != null)_callBack(null)
		}
		/**
		 *页数改变 
		 * @param e
		 * 
		 */		
		private function pageChangeHandler(e :PageEvent):void
		{
			if(selectItem)
			{
				selectItem.unSelect();
				selectItem = null;
			}
			//显示数据
			setItemPageDatas(ArrayUtil.getPageSizeDatas(pageComp.curPage,pageComp.pageSize,_goodsList));
		}
		/**
		 *双击使用物品 
		 * @param e
		 * 
		 */		
		private function itemDoubleClickHandler(e :ListEvent):void
		{
			var goodsVo :GoodsVo = e.item as GoodsVo;
			if(goodsVo == null)
			{
				return;
			}
			//直接调用背包使用物品接口（士气等）
			//士气卡判断
			//			if(goodsVo.type == GoodsType.OTHER 
			//				&& goodsVo.ntype == OtherGoodType.MORALE)
			//			{
			//				AppEventDispatcher.getInstance().dispatchEvent(new BagEvent(BagEvent.USE_ITEM,goodsVo));
			//				return;
			//			}
			if(_callBack != null)_callBack(goodsVo);
			TipsManager.hide();
		}
		
		/**
		 *物品选中 
		 * @param e
		 * 
		 */		
		private function itemClickHandler(e :ListEvent):void
		{
			if(selectItem)
			{
				selectItem.unSelect();
				selectItem = null;
			}
			selectItem = e.target as GoodsUseItem;
			selectItem.select();
			TipsManager.hide();
		}
		
		/**
		 *删除 
		 * 
		 */		
		public function removeEvtListener():void
		{
			pageComp.removeEventListener(PageEvent.PAGE_CHANGE,pageChangeHandler);
			this.removeEventListener(ListEvent.ITEM_CLICK,itemClickHandler);
			this.removeEventListener(ListEvent.ITEM_DOUBLE_CLICK,itemDoubleClickHandler);
			euroBtn.removeEventListener(MouseEvent.CLICK,onEuroHandler);
			this.removeEventListener(Event.REMOVED_FROM_STAGE,onremoveStageHandler);
			coinBtn.removeEventListener(MouseEvent.CLICK,onCoinHandler);
			useBtn.removeEventListener(MouseEvent.CLICK,onUseItemHandler);
			AppData.getInstance().bagDataManager.removeEventListener(Event.CHANGE,onGoodsChange);
			
			treatBtn.removeEventListener(MouseEvent.CLICK,onEuroHandler);
			recoverBtn.removeEventListener(MouseEvent.CLICK,onEuroHandler);
		}
		/**
		 * 移除面板 
		 * 
		 */
		public function rmoveStage():void
		{
			if(this.parent)
				this.parent.removeChild(this);
		}
		/**
		 *销毁 
		 */		
		public function destroyView():void
		{
			pageComp = null;
			_goodsList = null;
			for each(var item :GoodsUseItem in itemList)
			{
				item.destroyView();
			}
			itemList = null;
			_callBack = null;
		}

		/**
		 *回调 
		 */
		public function get callBack():Function
		{
			return _callBack;
		}

		/**
		 * @private
		 */
		public function set callBack(value:Function):void
		{
			_callBack = value;
		}

		
		/**
		 * 
		 */
		public static function get inistance():GoodsUsePanel
		{
			if(_inistance == null)
			{
				_inistance = new GoodsUsePanel();
				_inistance.initUI();
			}
			return _inistance;
		}


	}
}